﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Widgetsphere.WebUtils;
using System.Text;
using Widgetsphere.Autos.Websitev2.Objects;
using Widgetsphere.Autos.API.FacetedNavigation;

namespace Widgetsphere.Autos.Websitev2
{
    public partial class auto_detail : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Populate();
        }

        private void Populate()
        {
            var id = this.Request["id"].ToInt32();
            if (id == null)
            {
                this.Response.Redirect("/");
                return;
            }

            var query = new ListingQuery();
            query.FieldFilters.Add(new FieldFilterAutoID() { AutoID = id, Comparer = HP.Datastore.Common.ComparisonConstants.Equals });
            var results = RepositoryHelper.Query(query);
            var item = results.RecordList.FirstOrDefault();

            var imageURL = "/images/nophoto.jpg";
            var newPic = "/images/stock/" + item.Make.FormatCarImageFile() + "_" + item.Model.FormatCarImageFile() + ".jpg";
            var realFile = Server.MapPath(newPic);
            if (System.IO.File.Exists(realFile))
                imageURL = newPic;

            lblImage.ImageUrl = imageURL;

            lblHeader.Text = item.Year + " " + item.Make + " " + item.Model;
            lblMake.Text = item.Make;
            lblMileage.Text = item.Mileage.Value.ToString("###,###,##0");
            lblModel.Text = item.Model;
            lblPrice.Text = item.Price.Value.ToString("###,###,##0");
            lblState.Text = item.State;
            lblYear.Text = item.Year.ToString();
            lblDescription.Text = item.Description;

            MapControl1.ZoomLevel = 13;
            this.MapControl1.Populate(results, false);

        }

    }
}